﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;  
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DataAccess;
using Model;
using Presenters.WPF;
using Presenters.WPF.Views;

namespace AirlinePilotAcademy_WPF.UI_Administrator.Students
{
    /// <summary>
    /// Interaction logic for NewInscription.xaml
    /// </summary>
    public partial class NewInscription : Window, INewInscriptionView
    {
        NewInscriptionPresenter presenter;
        Student Sinscription = new Student();
        Inscription SinscriptionModify = new Inscription();
        public NewInscription()
        {
            InitializeComponent();
            presenter = new NewInscriptionPresenter(this);
            BindSubjectsCB(this.subjectCB, Sinscription.id);
            
            
        }
        public NewInscription(Student Sinscription)
        {
            InitializeComponent();
            presenter = new NewInscriptionPresenter(this);
            BindSubjectsCB(this.subjectCB,Sinscription.id);
            this.Sinscription = Sinscription;
            this.idTB.Text = Sinscription.id.ToString();
            this.nameTB.Text = Sinscription.name.ToString();
            this.lastnameTB.Text = Sinscription.lastname.ToString();
            this.score1TB.Text = "0";
            this.score2TB.Text = "0";
            this.score3TB.Text = "0";
            //this.SelectStudentB.IsEnabled = false;
        }
        private void BindSubjectsCB(ComboBox cb, int studentId)
        {
            //SqlDataAdapter da = new SqlDataAdapter("Select codMateria FROM Materia m WHERE codMateria NOT IN (SELECT codMateria FROM Inscripcion i WHERE ciEstudiante=@ciEstudiante and m.codMateria=i.codMateria)",conn);
           
            SqlConnection conn = new SqlConnection(Conexion.DataSource);
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "Select nombre FROM Materia m WHERE codMateria NOT IN (SELECT codMateria FROM Inscripcion1 i WHERE ciEstudiante=@ciEstudiante and m.codMateria=i.codMateria)";
            cmd.Parameters.AddWithValue("ciEstudiante", studentId);
            SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, conn);
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds, "Materia");
            cb.ItemsSource = ds.Tables[0].DefaultView;
            cb.DisplayMemberPath = ds.Tables[0].Columns["nombre"].ToString();
        }
        public int ciEstudiante
        {
            get { return Convert.ToInt32(this.idTB.Text); }
        }
        public long codMateria
        {
            get { return SubjectDataAccess.GetONESubjectByName(this.subjectCB.Text).code; }
        }
        public string gestion
        {
            get { return this.monthCB.Text; }
        }
        public long codI
        {
            get { return Convert.ToInt64(this.codTB.Text); }
        }
        public int score1
        {
            get { return Convert.ToInt16(this.score1TB.Text); }
        }
        public int score2
        {
            get { return Convert.ToInt16(this.score2TB.Text); }
        }
        public int score3
        {
            get { return Convert.ToInt16(this.score3TB.Text); }
        }
        public string message
        {
            set { messageL.Content = value; }
        }
        private void SaveB_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                presenter.SubmitNewInscription();
                MessageBox.Show("Puede añadir las notas de la materia de este estudiante entrando a la opcion Modificar Inscripcion");
                this.Close();
            }
            catch (Exception ex)
            {
                messageL.Content = ex.Message;
            }
        }

        private void CancelB_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void SelectStudentB_Click(object sender, RoutedEventArgs e)
        {
            SearchStudents ss = new SearchStudents(true);
            ss.ShowDialog();
            this.Close();
        }

       //MODIFY STUDENT INSCRIPTION
        public NewInscription(Inscription SinscriptionModify, string isModify)
        {
            InitializeComponent();
            this.SinscriptionModify = SinscriptionModify;
            presenter = new NewInscriptionPresenter(this);
            this.titleL.Content = "Modificar Inscripcion";
            this.Title = "Modificar Inscripcion";
            this.SelectStudentB.Visibility = Visibility.Hidden;
            this.ModifyB.Visibility = Visibility.Visible;
            this.SaveB.Visibility = Visibility.Hidden;

            this.codTB.Text = SinscriptionModify.codI.ToString();
            BindSubjectsCB(this.subjectCB,Sinscription.id);
            FillTBForModify();
        }

        private void FillTBForModify()
        {
            this.idTB.Text = SinscriptionModify.ciEstudiante.ToString();
            this.nameTB.Text = SinscriptionModify.Nombre;
            this.lastnameTB.Text = SinscriptionModify.Apellido;
            this.monthModifyTB.Text = SinscriptionModify.Gestion;
            this.subjectModifyTB.Text = SinscriptionModify.Materia.ToString();
            this.score1TB.Text = SinscriptionModify.score1.ToString();
            this.score2TB.Text = SinscriptionModify.score2.ToString();
            this.score3TB.Text = SinscriptionModify.score3.ToString();

            this.monthModifyTB.Visibility = Visibility.Visible;
            this.subjectModifyTB.Visibility = Visibility.Visible;

            this.score1TB.IsEnabled = true;
            this.score2TB.IsEnabled = true;
            this.score3TB.IsEnabled = true;
        }
        private void ModifyB_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                presenter.SubmitModifyInscription();
                this.Close();
            }
            catch (Exception ex)
            {
                messageL.Content = ex.Message;
            }
        }
        //VIEW INSCRIPTION
        public NewInscription(Inscription SinscriptionView, string isView1,string isView)
        {
            InitializeComponent();
            presenter = new NewInscriptionPresenter(this);
            DisableAllCampusANDButtons();

            this.Title = "Ver Inscripcion";
            this.titleL.Content = "Ver Inscripcion";
            this.SelectStudentB.Visibility = Visibility.Hidden;

            this.codTB.Text = SinscriptionView.codI.ToString();
            this.nameTB.Text = SinscriptionView.Nombre;
            this.lastnameTB.Text = SinscriptionView.Apellido;
            this.subjectTB.Text = SinscriptionView.Materia;
            this.monthTB.Text = SinscriptionView.Gestion;
            this.idTB.Text = SinscriptionView.ciEstudiante.ToString();
            this.score1TB.Text = SinscriptionView.score1.ToString();
            this.score2TB.Text = SinscriptionView.score2.ToString();
            this.score3TB.Text = SinscriptionView.score3.ToString();

        }
        private void DisableAllCampusANDButtons()
        {
            this.idTB.IsEnabled = false;
            this.nameTB.IsEnabled = false;
            this.lastnameTB.IsEnabled = false;

            this.monthTB.Visibility = Visibility.Visible;
            this.subjectTB.Visibility = Visibility.Visible;

            this.monthCB.Visibility = Visibility.Hidden;
            this.subjectCB.Visibility = Visibility.Hidden;

            this.SelectStudentB.IsEnabled = false;
            this.SaveB.Visibility = Visibility.Hidden;
            this.CancelB.Content = "Cerrar";
        }
        
    }
}
